﻿using System;
using System.IO;
using System.Web.UI.WebControls;
using BONSAI.BLL;


namespace BONSAI.Admin.UC
{
    public partial class PhotoManager : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindStart();
            }
        }
        public void BindStart()
        {
            // delete all
            if (Request.QueryString["zxc"] != null)
            {
                if (Request.QueryString["zxc"].ToString() == "qwertyuiop")
                {
                    if (Request.QueryString["id"] != null)
                    {
                        //im.ImageTours_Delete(0);
                    }
                }
            }
            //============
            if (Request.QueryString["type"] != null)
            {
                //DataTable table = im.ImageTours_Get(Int32.Parse(Request.QueryString["type"].ToString()),0);
                var table = (new PhotosBO()).GetByProductId(Request.QueryString["type"]);
                GridView1.DataSource = table;
                GridView1.DataBind();
                
                // thong tin album
                var productDto = (new ProductsBo()).GetById(Request.QueryString["type"]);
                if (productDto != null)
                {
                    Label1.Text = productDto.NameFull;
                }
            }
            // 
        }
        protected void lbcreate_Click(object sender, EventArgs e)
        {
            Response.Redirect("PhotoUpdate.aspx?type=" + Request.QueryString["type"].ToString());
        }

        protected void LinkButton1_Click(object sender, EventArgs e)// xoa
        {
           // HtmlInputCheckBox check;
           //// bool res = false;

           // foreach (GridViewRow row in GridView1.Rows)
           // {
           //     check = (HtmlInputCheckBox)row.FindControl("checkbox");
           //     if (check.Checked == true)
           //     {
           //         em.Photo_Delete(Int32.Parse(check.Value.ToString()));
           //        // res = true;
           //     }
           // }
           // //if (res == false)
           // //{
           // //    WebMBox.Show("Vui lòng chọn ảnh, Check phần màu đỏ để chọn ảnh.");
           // //}
           // BindStart();
        }

        protected void btedit_Click(object sender, EventArgs e)
        {
            //HtmlInputCheckBox check;
            //bool res = false;
            //string str = "";
            //int count = 0;
            //foreach (GridViewRow row in GridView1.Rows)
            //{
            //    check = (HtmlInputCheckBox)row.FindControl("checkbox");
            //    if (check.Checked == true)
            //    {
            //        str = check.Value.ToString();
            //        count++;
            //        res = true;
            //    }
            //}
            //if (res == false)
            //{
            //    WebMBox.Show("Vui lòng chọn ảnh, Check phần màu đỏ để chọn ảnh.");
            //    return;
            //}
            //else
            //{
            //    if (count > 1)
            //    {
            //        WebMBox.Show("Bạn chọn nhiều hơn 1 ảnh.");
            //        return;

            //    }
            //    Response.Redirect("PhotoUpdate.aspx?id=" + Request.QueryString["id"].ToString() + "&photo=" + str);
            //}
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindStart();
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            var dataKey = GridView1.DataKeys[e.NewEditIndex];
            if (dataKey != null)
            {
                string key = dataKey.Value.ToString();
                Response.Redirect("PhotoUpdate.aspx?type=" + Request.QueryString["type"] + "&id=" + key);
            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            var dataKey = GridView1.DataKeys[e.RowIndex];
            if (dataKey != null)
            {
                string key = dataKey.Value.ToString();
                //get image of proructs
                var photosDto = (new PhotosBO()).GetByID(key);
                if(photosDto != null)
                {
                        try
                        {
                            File.Delete(Server.MapPath(photosDto.thumbnail));
                            File.Delete(Server.MapPath(photosDto.preview));
                        }
                        catch (Exception ex)
                        {
                          throw ex;
                        }
                    (new PhotosBO()).DeleteByID(key);

                }
            }
            //im.ImageTours_Delete(Int32.Parse(key));
            BindStart();
        }
    }
}